<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class M_login extends BZ_Model
{
/**
 * @section biskitz Login Model
 */

	/**
	 * @function _logout()
	 * @description logout the current user
	 */
	function _logout($prm_dbase = 'db_default', $prm_action_note = 'logout')
	{
		/**
		 * @description clear template cache
		 */
		if ($this->web_data['BISKITZ']['SETTINGS']['BISKITZ_CACHE_DESTROY_ON_LOGOUT'] == TRUE)
		{
			$this->bz_tlite->clear_cache(NULL, 'admin_user|'.$this->session->userdata('session_user_id'));
		}

		/**
		 * @description log the action!
		 */
		$this->M_log->write('db_default', $prm_action_note);


		$this->session->sess_destroy();
	}


	/**
	 * @function check()
	 * @description check the login, valid or not
	 */
	function check($prm_dbase = 'db_default', $prm_user_id = '', $prm_password = '')
	{
		$use_db = $this->{$prm_dbase};

		$sql = 'select a.id as user_id, a.name as user_name, a.menu_module_use as menu_module_use'
			. ', b.id as lang_id, b.name as lang_name, b.abbreviation as lang_abbr'
			. ' from bz_biskitz_user a, bz_biskitz_language b'
			. ' where a.language__id = b.id and a.id = ? and pass = ?';
		$qry = $use_db->query
			(
				$sql
				, array('id' => $prm_user_id, 'pass' => md5($prm_password))
			);

		if ($qry->num_rows() > 0)
		{
			$session_biskitz = array
				(
					'session_login_stat' => TRUE
					, 'session_user_id' => htmlentities($qry->row()->user_id)
					, 'session_user_name' => htmlentities($qry->row()->user_name)
					, 'session_user_menu_modul_use' => htmlentities($qry->row()->menu_module_use)
					, 'session_user_language_id' => htmlentities($qry->row()->lang_id)
					, 'session_user_language_name' => htmlentities($qry->row()->lang_name)
					, 'session_user_language_abbr' => htmlentities($qry->row()->lang_abbr)
				);

			$this->session->set_userdata($session_biskitz);

			/**
			 * @description log the action!
			 */
			$this->M_log->write($prm_dbase, 'login');



			/**
			 * @section Add Another Session Module
			 * @description add another session for other modules (if modules exists!)
			 */

				/**
				 * @description TODO: should check if there is a loaded module for that user or not AND the session should be done with loops!
				 */
				// this is just workaround for hris
				$sql = 'select data__id, number, company__id, structure__id, work_location__id'
					. ' from bz_hris_employee_company'
					. ' where data__id = ? and termination__id is NULL'; // nanti where data__id ganti jadi where number
				$qry = $use_db->query
					(
						$sql
						, array('id' => $prm_user_id)
					);


				if ($qry->num_rows() > 0)
				{
					$session_hris = array
						(
							'session_hris_employee_id' => $qry->row()->data__id
							, 'session_hris_employee_number' => $qry->row()->number
							, 'session_hris_employee_company_id' => $qry->row()->company__id
							, 'session_hris_employee_structure_id' => $qry->row()->structure__id
							, 'session_hris_employee_work_location_id' => $qry->row()->work_location__id
						);

					$this->session->set_userdata($session_hris);
				}


			return TRUE;

		}
		else
		{
			return FALSE;
		}
	}


	/**
	 * @function session_check()
	 * @description check the session, already expired ?
	 */
	function session_check()
	{
		@session_start();
		switch ($this->session->userdata('session_login_stat'))
		{
			case TRUE:
				$this->web_data['BISKITZ']['SESSION'] = array('STATUS' => $this->session->userdata('session_login_stat'));
				break;

			default:
				$this->_logout();

				die ('<span style="font: 9pt Sans, Verdana; font-weight: bold;">'.$this->lang->line('login_logout')
							.'</span><script>location.replace("'.site_url('admin').'");</script>');
				break;
		}
	}



}

/** End of file m_login.php */
/** Location: ./biskitz_mpc/biskitz/models/admin/m_login.php */
